package com.bear.maker.generator.main;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.StrUtil;
import com.bear.maker.generator.file.DynamicFileGenerator;
import com.bear.maker.meta.Meta;
import com.bear.maker.meta.MetaManager;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.IOException;

/**
 * @Author : bear
 * Description :
 * @Create : 2024/11/14 - 20:16
 */
public class MainGenerator {
    public static void main(String[] args) throws TemplateException, IOException {
        Meta meta = MetaManager.getMetaObject();
//        System.out.println(meta);

        //输出根路径
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generator" + File.separator + meta.getName();
        if (!FileUtil.exist(outputPath)) {
            FileUtil.mkdir(outputPath);
        }
        //X:\bear-generator\bear-generator-maker\generator\acm-template-pro-generator
//        System.out.println("outputPath:" + outputPath);

        //读取resources目录,模板路径
        ClassPathResource classPathResource = new ClassPathResource("");
//        System.out.println(classPathResource);
        String inputResourcePath = classPathResource.getAbsolutePath();
//        System.out.println(inputResourcePath);

        //Java包基础路径
        String outputBasePackage = meta.getBasePackage();//com.bear
//        System.out.println(outputBasePackage);
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
//        System.out.println("outputBasePackagePath:"+outputBasePackagePath);//com/bear
        String outputBaseJavaPackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
//        System.out.println("outputBaseJavaPackagePath:" + outputBaseJavaPackagePath);
        //X:\bear-generator\bear-generator-maker\generator\acm-template-pro-generator\src/main/java/com/bear

        String inputFilePath;
        String outputFilePath;

        //model.DataModel
        inputFilePath = inputResourcePath + "templates/java/model/DataModel.java.ftl";
//        System.out.println(inputFilePath);
        outputFilePath = outputBaseJavaPackagePath + "/model/DataModel.java";
//        System.out.println("outputFilePath:"+outputFilePath);
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, meta);

        //cli.command.ConfigCommand
        inputFilePath = inputResourcePath + "templates/java/cli/command/ConfigCommand.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + "/cli/command/ConfigCommand.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, meta);

        //cli.command.GenerateCommand
        inputFilePath = inputResourcePath + "templates/java/cli/command/GenerateCommand.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + "/cli/command/GenerateCommand.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, meta);

        //cli.command.ListCommand
        inputFilePath = inputResourcePath + "templates/java/cli/command/ListCommand.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + "/cli/command/ListCommand.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, meta);

        //cli.CommandExecutor
        inputFilePath = inputResourcePath + "templates/java/cli/CommandExecutor.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + "/cli/CommandExecutor.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, meta);

        //Main
        inputFilePath = inputResourcePath + "templates/java/Main.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + "/Main.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, meta);
    }
}
